//
//  XHQArchiverTool.m
//  XiongMaoJiaSu
//
//  Created by 徐汉卿 on 16/6/1.
//  Copyright © 2016年 ISOYasser. All rights reserved.
//

#import "XHQArchiverTool.h"

NSString *const kFilePathUserModel = @"lkFilePathUserModel";


NSString *const kFilePathNoReadModel = @"kFilePathNoReadModel";

NSString *const kNotifNoReadModelChang = @"kNotifNoReadModelChang";


//
NSString *const kFilePathNoReadFankuiModel = @"kNoReadFankuiModel";
NSString *const kFilePathNoReadXitongModel  = @"kNoReadXitongModel";

@implementation XHQArchiverTool

+(void)writeModel:(id)model to:(NSString *)path
{
  
    
    // 创建归档时所需的data 对象.
    NSMutableData *data = [NSMutableData data];
    
    // 归档类.
    NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data];
    
    // 开始归档（@"model" 是key值，也就是通过这个标识来找到写入的对象）.
    [archiver encodeObject:model forKey:@"model"];
    
    // 归档结束.
    [archiver finishEncoding];
    
    // 写入本地（@"weather" 是写入的文件名）.
    NSString *file = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:path];
    
    [data writeToFile:file atomically:YES];
    
//    //发通知
//    if ([path isEqualToString:kFilePathNoReadFankuiModel] || [path isEqualToString:kFilePathNoReadXitongModel]) {
//          [[NSNotificationCenter defaultCenter] postNotificationName: kNotifNoReadModelChang object:nil];
//    }
}



+(id)readModelWithPath:(NSString *)path
{
    NSString *file = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:path];
    
    // data.
    NSData *data = [NSData dataWithContentsOfFile:file];
    
    // 反归档.
    NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:data];
    
    // 获取@"model" 所对应的数据
    id model = [unarchiver decodeObjectForKey:@"model"];
    
    // 反归档结束.
    [unarchiver finishDecoding];
    return model;
}
@end
